package com.fuzongle.tank.common.mobile;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;



/**
 * 
 * @Description:获取地理位置-工具类
 * @author: fuzongle   
 * @date: 2019年3月12日 下午6:43:24
 */
public class Mobile {
	
	public static final String URl="http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=";


	public static String show2(String mobile) throws ParseException, IOException {
		try {
			String provinceName=null;
			HttpClientResult httpClientResult = doGet(URl+ mobile);
			if (httpClientResult.getCode()==200){
				//第一次解析 {"response":{"15800000026":{"location":"广东广州移动","detail":{"area":[{"city":"广州"}],"province":"广东","type":"domestic","operator":"移动"}}}
				JSONObject jsonObject = JSONObject.parseObject(httpClientResult.getContent());
				//第二次解析  {"15800000026":{"location":"广东广州移动","detail":{"area":[{"city":"广州"}],"province":"广东","type":"domestic","operator":"移动"}}}
				JSONObject deleteResponse = jsonObject.getJSONObject("response");
				//第三次解析
				JSONObject deletePhonce = deleteResponse.getJSONObject(mobile);
				//第四次解析
				JSONObject detail = deletePhonce.getJSONObject("detail");
				//获取省的名字
				String string = detail.getString("area");
/*				provinceName= detail.getString("province");
*/				//获取市区的名字  
				JSONArray parseArray = JSONArray.parseArray(string);
				JSONObject object = parseArray.getJSONObject(0);
				provinceName = object.getString("city");
				 }
			return provinceName;

			} catch (Exception e) {
				e.printStackTrace();
			}
		return mobile;
}
	public static String show(String mobile) throws ParseException, IOException {
			try {
				String provinceName=null;
				HttpClientResult httpClientResult = doGet("" + mobile);
				if (httpClientResult.getCode()==200){
					//第一次解析 {"response":{"15800000026":{"location":"广东广州移动","detail":{"area":[{"city":"广州"}],"province":"广东","type":"domestic","operator":"移动"}}}
					JSONObject jsonObject = JSONObject.parseObject(httpClientResult.getContent());
					//第二次解析  {"15800000026":{"location":"广东广州移动","detail":{"area":[{"city":"广州"}],"province":"广东","type":"domestic","operator":"移动"}}}
					JSONObject deleteResponse = jsonObject.getJSONObject("response");
					//第三次解析
					JSONObject deletePhonce = deleteResponse.getJSONObject(mobile);
					//第四次解析
					JSONObject detail = deletePhonce.getJSONObject("detail");
					//获取省的名字
					 provinceName = detail.getString("province");
					 System.out.println(provinceName);}
					 return provinceName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 发送get请求；不带请求头和请求参数
	 *
	 * @param url 请求地址
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult doGet(String url) throws Exception {
		return doGet(url, null, null);
	}
	public static HttpClientResult doGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
		// 创建httpClient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();

		// 创建访问的地址
		URIBuilder uriBuilder = new URIBuilder(url);
		if (params != null) {
			Set<Map.Entry<String, String>> entrySet = params.entrySet();
			for (Map.Entry<String, String> entry : entrySet) {
				uriBuilder.setParameter(entry.getKey(), entry.getValue());
			}
		}

		// 创建http对象
		HttpGet httpGet = new HttpGet(uriBuilder.build());
		/**
		 * setConnectTimeout：设置连接超时时间，单位毫秒。
		 * setConnectionRequestTimeout：设置从connect Manager(连接池)获取Connection
		 * 超时时间，单位毫秒。这个属性是新加的属性，因为目前版本是可以共享连接池的。
		 * setSocketTimeout：请求获取数据的超时时间(即响应时间)，单位毫秒。 如果访问一个接口，多少时间内无法返回数据，就直接放弃此次调用。
		 */
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000).setSocketTimeout(6000).build();
		httpGet.setConfig(requestConfig);

		// 设置请求头
		packageHeader(headers, httpGet);

		// 创建httpResponse对象
		CloseableHttpResponse httpResponse = null;

		try {
			// 执行请求并获得响应结果
			HttpClientResult httpClientResult = getHttpClientResult(httpResponse, httpClient, httpGet);
			return httpClientResult;
		} finally {
			// 释放资源
			release(httpResponse, httpClient);
		}
	}
	/**
	 * Description: 封装请求头
	 *
	 * @param params
	 * @param httpMethod
	 */
	public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
		// 封装请求头
		if (params != null) {
			Set<Map.Entry<String, String>> entrySet = params.entrySet();
			for (Map.Entry<String, String> entry : entrySet) {
				// 设置到请求头到HttpRequestBase对象中
				httpMethod.setHeader(entry.getKey(), entry.getValue());
			}
		}
	}
	/**
	 * Description: 获得响应结果
	 *
	 * @param httpResponse
	 * @param httpClient
	 * @param httpMethod
	 * @return
	 * @throws Exception
	 */
	public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
		// 执行请求
		httpResponse = httpClient.execute(httpMethod);

		// 获取返回结果
		if (httpResponse != null && httpResponse.getStatusLine() != null) {
			String content = "";
			if (httpResponse.getEntity() != null) {
				content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
			}
			return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
		}
		return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
	}

	/**
	 * Description: 释放资源
	 *
	 * @param httpResponse
	 * @param httpClient
	 * @throws IOException
	 */
	public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
		// 释放资源
		if (httpResponse != null) {
			httpResponse.close();
		}
		if (httpClient != null) {
			httpClient.close();
		}
	}
	
	public static void main(String[] args) throws ParseException, IOException {
		String show2 = show2("15937016361");
		System.out.println("手机号的位置"+show2);
		/*try { 	
			String substring = show.substring(54, 56);
			System.out.println(show);
		} finally {
		}*/
	}
}